export const replaceTags = (text) => {
  return text.replace(/<.*?>/g, '');
};

// 校验特殊字符
export const checkSpecialKey = (str) => {
  // let reg = new RegExp("^[A-Za-z0-9\u4e00-\u9fa5]+$");//中文英文数字
  // return reg.test(str)
  var specialKey = "[`~!#$^&()=|{}':;'\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？%]‘'";
  for (var i = 0; i < str.length; i++) {
    if (specialKey.indexOf(str.substr(i, 1)) != -1) {
      return false;
    }
  }
  return true;
};

export const clearValue = (obj) => {
  try {
    Object.keys(obj).forEach((key) => {
      if (typeof obj[key] == 'object') {
        clearValue(obj[key]);
      } else {
        obj[key] = '';
      }
    });
  } catch (e) {
    console.error(e);
  }
};

/**
 * 校验字符串是否为空、null、undefined
 * @param {字符串} str
 * @returns
 */
export const isEmpty = (str) => {
  if (str === '' || str === null || str === undefined) {
    return true;
  }
  if (typeof str === 'string') {
    if (str.trim() === '') {
      return true;
    }
  }
  return false;
};

/**
 * 校验字符串是否为空、null、undefined
 * @param {字符串} str
 * @returns
 */
export const isNotEmpty = (str) => {
  if (str === '' || str === null || str === undefined) {
    return false;
  }
  if (typeof str === 'string') {
    if (str.trim() === '') {
      return false;
    }
  }

  return true;
};

/**
 * 对象a从对象b中取值
 * @param {*} arra
 * @param {*} arrb
 * @returns
 */
export const listAssign = (arra, arrb) =>
  Object.keys(arra).forEach((key) => {
    arra[key] = arrb[key] || arra[key];
  });

/**
 * 生成UUID
 * @returns uuid
 */
export const uuid = () => {
  var s = [];
  var hexDigits = '0123456789abcdef';
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = '-';
  var uuid = s.join('');
  return uuid;
};

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
  let date;
  if (typeof time === 'object') {
    date = time;
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000;
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value];
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value;
    }
    return value || 0;
  });
  return time_str;
}

export const list2tree = (list, parentId) => {
  list.sort((a, b) => {
    return a.id - b.id;
  });
  let obj = {};
  list.forEach((item) => {
    item.children = [];
    obj[item.id] = item;
  });
  return list.filter((item) => {
    if (item.parentId !== parentId) {
      obj[item.parentId].children.push(item);
      return false;
    }
    return true;
  });
};
